﻿using System;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo
{
	// Token: 0x0200001D RID: 29
	[AddComponentMenu("AVPro Video/Display IMGUI", 200)]
	[HelpURL("http://renderheads.com/product/avpro-video/")]
	[ExecuteInEditMode]
	public class DisplayIMGUI : MonoBehaviour
	{
		// Token: 0x060000AA RID: 170 RVA: 0x000074FC File Offset: 0x000056FC
		private void Awake()
		{
			if (DisplayIMGUI._propAlphaPack == 0)
			{
				DisplayIMGUI._propAlphaPack = Shader.PropertyToID("AlphaPack");
				DisplayIMGUI._propVertScale = Shader.PropertyToID("_VertScale");
				DisplayIMGUI._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
			}
		}

		// Token: 0x060000AB RID: 171 RVA: 0x00007538 File Offset: 0x00005738
		private void Start()
		{
			if (!this._useDepth)
			{
				base.useGUILayout = false;
			}
			if (DisplayIMGUI._shaderAlphaPacking == null)
			{
				DisplayIMGUI._shaderAlphaPacking = Shader.Find("AVProVideo/IMGUI/Texture Transparent");
				if (DisplayIMGUI._shaderAlphaPacking == null)
				{
					GlobalData.Debug_LogWarning("[AVProVideo] Missing shader AVProVideo/IMGUI/Transparent Packed");
				}
			}
		}

		// Token: 0x060000AC RID: 172 RVA: 0x00007590 File Offset: 0x00005790
		private void OnDestroy()
		{
			if (this._material != null)
			{
				UnityEngine.Object.Destroy(this._material);
				this._material = null;
			}
		}

		// Token: 0x060000AD RID: 173 RVA: 0x000075B8 File Offset: 0x000057B8
		private Shader GetRequiredShader()
		{
			Shader shader = null;
			AlphaPacking alphaPacking = this._mediaPlayer.m_AlphaPacking;
			if (alphaPacking != AlphaPacking.None)
			{
				if (alphaPacking == AlphaPacking.LeftRight || alphaPacking == AlphaPacking.TopBottom)
				{
					shader = DisplayIMGUI._shaderAlphaPacking;
				}
			}
			if (shader == null && this._mediaPlayer.Info != null && QualitySettings.activeColorSpace == ColorSpace.Linear && this._mediaPlayer.Info.PlayerSupportsLinearColorSpace())
			{
				shader = DisplayIMGUI._shaderAlphaPacking;
			}
			if (shader == null && this._mediaPlayer.TextureProducer != null && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
			{
				shader = DisplayIMGUI._shaderAlphaPacking;
			}
			return shader;
		}

		// Token: 0x060000AE RID: 174 RVA: 0x00007678 File Offset: 0x00005878
		private void Update()
		{
			if (this._mediaPlayer != null)
			{
				Shader x = null;
				if (this._material != null)
				{
					x = this._material.shader;
				}
				Shader requiredShader = this.GetRequiredShader();
				if (x != requiredShader)
				{
					if (this._material != null)
					{
						UnityEngine.Object.Destroy(this._material);
						this._material = null;
					}
					if (requiredShader != null)
					{
						this._material = new Material(requiredShader);
					}
				}
				if (this._material != null)
				{
					if (this._material.HasProperty(DisplayIMGUI._propAlphaPack))
					{
						Helper.SetupAlphaPackedMaterial(this._material, this._mediaPlayer.m_AlphaPacking);
					}
					if (this._material.HasProperty(DisplayIMGUI._propApplyGamma) && this._mediaPlayer.Info != null)
					{
						Helper.SetupGammaMaterial(this._material, !this._mediaPlayer.Info.PlayerSupportsLinearColorSpace());
					}
				}
			}
		}

		// Token: 0x060000AF RID: 175 RVA: 0x00007784 File Offset: 0x00005984
		private void OnGUI()
		{
			if (this._mediaPlayer == null)
			{
				return;
			}
			bool flag = false;
			Texture texture = null;
			if (this._displayInEditor)
			{
			}
			if (this._mediaPlayer.Info != null && !this._mediaPlayer.Info.HasVideo())
			{
				texture = null;
			}
			if (this._mediaPlayer.TextureProducer != null)
			{
				if (this._mediaPlayer.TextureProducer.GetTexture(0) != null)
				{
					texture = this._mediaPlayer.TextureProducer.GetTexture(0);
					flag = this._mediaPlayer.TextureProducer.RequiresVerticalFlip();
				}
				if (this._mediaPlayer.TextureProducer.GetTextureCount() == 2 && this._material != null)
				{
					Texture texture2 = this._mediaPlayer.TextureProducer.GetTexture(1);
					this._material.SetTexture("_ChromaTex", texture2);
					this._material.EnableKeyword("USE_YPCBCR");
				}
			}
			if (texture != null && (!this._alphaBlend || this._color.a > 0f))
			{
				GUI.depth = this._depth;
				GUI.color = this._color;
				Rect rect = this.GetRect();
				if (this._material != null)
				{
					if (flag)
					{
						this._material.SetFloat(DisplayIMGUI._propVertScale, -1f);
					}
					else
					{
						this._material.SetFloat(DisplayIMGUI._propVertScale, 1f);
					}
					Helper.DrawTexture(rect, texture, this._scaleMode, this._mediaPlayer.m_AlphaPacking, this._material);
				}
				else
				{
					if (flag)
					{
						GUIUtility.ScaleAroundPivot(new Vector2(1f, -1f), new Vector2(0f, rect.y + rect.height / 2f));
					}
					GUI.DrawTexture(rect, texture, this._scaleMode, this._alphaBlend);
				}
			}
		}

		// Token: 0x060000B0 RID: 176 RVA: 0x00007980 File Offset: 0x00005B80
		public Rect GetRect()
		{
			Rect result;
			if (this._fullScreen)
			{
				result = new Rect(0f, 0f, (float)Screen.width, (float)Screen.height);
			}
			else
			{
				result = new Rect(this._x * (float)(Screen.width - 1), this._y * (float)(Screen.height - 1), this._width * (float)Screen.width, this._height * (float)Screen.height);
			}
			return result;
		}

		// Token: 0x040000A3 RID: 163
		public MediaPlayer _mediaPlayer;

		// Token: 0x040000A4 RID: 164
		public bool _displayInEditor = true;

		// Token: 0x040000A5 RID: 165
		public ScaleMode _scaleMode = ScaleMode.ScaleToFit;

		// Token: 0x040000A6 RID: 166
		public Color _color = Color.white;

		// Token: 0x040000A7 RID: 167
		public bool _alphaBlend;

		// Token: 0x040000A8 RID: 168
		[SerializeField]
		private bool _useDepth;

		// Token: 0x040000A9 RID: 169
		public int _depth;

		// Token: 0x040000AA RID: 170
		public bool _fullScreen = true;

		// Token: 0x040000AB RID: 171
		[Range(0f, 1f)]
		public float _x;

		// Token: 0x040000AC RID: 172
		[Range(0f, 1f)]
		public float _y;

		// Token: 0x040000AD RID: 173
		[Range(0f, 1f)]
		public float _width = 1f;

		// Token: 0x040000AE RID: 174
		[Range(0f, 1f)]
		public float _height = 1f;

		// Token: 0x040000AF RID: 175
		private static int _propAlphaPack;

		// Token: 0x040000B0 RID: 176
		private static int _propVertScale;

		// Token: 0x040000B1 RID: 177
		private static int _propApplyGamma;

		// Token: 0x040000B2 RID: 178
		private static Shader _shaderAlphaPacking;

		// Token: 0x040000B3 RID: 179
		private Material _material;
	}
}
